<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\Admin\PermissionRequest;
use App\Models\Permission;
use App\Http\Controllers\Controller;

class PermissionController extends Controller
{
	/**
	 * Display a listing of the resource.
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function index()
	{
		$permissionList = Permission::query()
					->with(['parent'])
					->get()
					->toArray();

		$permissionList = getTree($permissionList);

		return view('admin.permission.index', compact('permissionList'));
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function create()
	{
		$permissionList = Permission::all()->toArray();

		$permissionList = getTree($permissionList);

		return view('admin.permission.create', compact('permissionList'));
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @param  \Illuminate\Http\Request $request
	 * @return \Illuminate\Http\Response
	 */
	public function store(PermissionRequest $request, Permission $permissionModel)
	{
		if ($request->method() == 'POST') {
			$insertData = $request->except('_token');

			//如果非顶级权限 则验证路由不能为空
			if($insertData['pid'] != '0'){

				$validator = \Validator::make($insertData,[
					'route_url'=>'required|max:150|string',//应该补充唯一验证
				],[
					'route_url.max'=>'路由长度不能超过150个字符',
					'route_url.string'=>'路由必须是字符串',
					'route_url.required'=>'权限路由必须填写',
				]);

				if ($validator->fails()) {
					return back()
						->withErrors($validator)
						->withInput();
				}
			}

			$permissionModel->fill($insertData);
			$permissionModel->save();

			return redirect()->route('admin.permission.index')->with('success', '添加成功');
		}
	}


	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int $id
	 * @return \Illuminate\Http\Response
	 */
	public function edit($id)
	{
		$permission = Permission::findOrFail($id);

		$permissionList = Permission::all()->toArray();
		$permissionList = getTree($permissionList);

		return view('admin.permission.edit',compact('permission','permissionList'));
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  \Illuminate\Http\Request $request
	 * @param  int $id
	 * @return \Illuminate\Http\Response
	 */
	public function update(PermissionRequest $request, $id)
	{
		if ($request->method() == 'POST') {
			$permission = Permission::findOrFail($id);

			$updateData = $request->except('_token');

			//判断自己的父类是不是自己的子类
			$id = $updateData['id'];
			$childIds = getChildIds('permission',$id);
			$childIds[] = $id;

			$pid = $updateData['pid'];

			if(in_array($pid,$childIds)){
				return back()->with('danger','上级不能是自己的子分类或者自己');
			}

			$permission->fill($updateData);
			$permission->update();

			return redirect()->route('admin.permission.index')->with('success', '修改成功');
		}
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int $id
	 * @return \Illuminate\Http\Response
	 */
	public function destroy($id)
	{
		//判断是否有下级
		$children = getChildIds('permission',$id);

		if($children){
			return back()->with('warning','权限有下级权限，请先删除下级权限');
		}

		$persmission = Permission::findOrFail($id);

		$persmission->role_permission()->update([
				'role_permission.deleted_at'=>date('Y-m-d H:i:s',time())
		]);

		$persmission->delete();


		return back()->with('success','删除成功');

	}
}
